package com.jeff.android.lab.module.thirdwidget.stickyheader;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jeff.android.lab.R;

import org.zakariya.stickyheaders.SectioningAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: dingfeng
 * @date: 2019-11-28
 * @since: 1.0.0
 **/
public class NameListAdapter extends SectioningAdapter {

    private class Section {
        String index;
        List<Name> items = new ArrayList<>();
    }

    private List<Section> convertSourceData2Section(List<Name> data) {
        List<String> indexes = new ArrayList<>();
        for (Name n : data) {
            String index = n.index;
            if (indexes.contains(index)) {
                for (Section s : sections) {
                    if (index.equals(s.index)) {
                        s.items.add(n);
                        break;
                    }
                }
            } else {
                Section section = new Section();
                section.index = index;
                section.items = new ArrayList<>();
                section.items.add(n);
                indexes.add(index);

                sections.add(section);
            }
        }

        return sections;
    }

    private List<Section> sections = new ArrayList<>();

    public NameListAdapter() {
    }

    public NameListAdapter(List<Name> names) {
        this.sections = convertSourceData2Section(names);
    }

    public void addData(List<Name> names) {
        convertSourceData2Section(names);

        notifyAllSectionsDataSetChanged();
    }

    @Override
    public GhostHeaderViewHolder onCreateGhostHeaderViewHolder(ViewGroup parent) {
        View ghostView = new View(parent.getContext());
        ghostView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return new GhostHeaderViewHolder(ghostView);
    }

    @Override
    public int getNumberOfSections() {
        return sections.size();
    }

    @Override
    public int getNumberOfItemsInSection(int sectionIndex) {
        return sections.get(sectionIndex).items.size();
    }

    @Override
    public boolean doesSectionHaveHeader(int sectionIndex) {
        return true;
    }



    /*
    * Header
    * */

    @Override
    public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerUserType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.thirdwidget_item_simple_name_header, parent, false);
        return new HeaderHolder(v);
    }

    @Override
    public void onBindHeaderViewHolder(HeaderViewHolder viewHolder, int sectionIndex, int headerUserType) {
        Section s = sections.get(sectionIndex);
        HeaderHolder ivh = (HeaderHolder) viewHolder;
        ivh.tvName.setText(s.index);
    }

    public static class HeaderHolder extends SectioningAdapter.HeaderViewHolder {
        TextView tvName;
        TextView btnFilter;

        public HeaderHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            btnFilter = itemView.findViewById(R.id.btn_filter);
        }
    }

    /*
    * Item
    * */

    @Override
    public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemUserType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.thirdwidget_item_simple_name, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindItemViewHolder(SectioningAdapter.ItemViewHolder viewHolder, int sectionIndex, int itemIndex, int itemUserType) {
        Section s = sections.get(sectionIndex);
        ItemViewHolder ivh = (ItemViewHolder) viewHolder;
        ivh.tvName.setText(s.items.get(itemIndex).firstName);
    }

    public class ItemViewHolder extends SectioningAdapter.ItemViewHolder {
        TextView tvName;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }

}
